﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VideoCache.Server.BLL;
using VideoCache.Server.MVCWeb.Attributes;
using VideoCache.Server.MVCWeb.Areas.Admin.Models;
using VideoCache.Server.Model;

namespace VideoCache.Server.MVCWeb.Areas.Admin.Controllers
{
    [ActionAuthorize(AuthorizeType.Administrator)]
    public class SiteController : Controller
    {
        //
        // GET: /Admin/Site/

        public ActionResult Index()
        {
            var list = SiteBLL.FindAll(null, null);

            return View(list);
        }

        public ActionResult Delete(int id)
        {
            SiteBLL.Delete(id);

            return RedirectToAction("Index");
        }

        public ActionResult Create()
        {
            return View(new SiteModel());
        }

        [HttpPost]
        public ActionResult Create(SiteModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    SiteInfo info = new SiteInfo
                    {
                        Name = model.Name,
                        UrlHost = model.UrlHost,
                        DisplayName = model.DisplayName,
                        AllowDownload = model.AllowDownload ? (ulong)1 : (ulong)0,
                        Downloader = model.Downloader,
                        PlayerId = model.PlayerId,
                        PositionTag = model.PositionTag,
                        CreateTime = DateTime.Now,
                        ModifyTime = DateTime.Now
                    };

                    SiteBLL.Add(info);

                    return RedirectToAction("Index");
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("Error", ex.Message);
                }
            }

            return View(model);
        }

        public ActionResult Edit(int id)
        {
            SiteInfo info = SiteBLL.Find(id);

            if (info == null) return HttpNotFound();

            SiteModel model = new SiteModel
            {
                Id = info.Id,
                Name = info.Name,
                UrlHost = info.UrlHost,
                DisplayName = info.DisplayName,
                AllowDownload = info.AllowDownload > 0 ? true : false,
                Downloader = info.Downloader,
                PlayerId = info.PlayerId,
                PositionTag = info.PositionTag
            };

            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(SiteModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    SiteInfo info = SiteBLL.Find(model.Id);
                    if (info == null) throw new ArgumentException("指定的站点不存在。");

                    info.Id = model.Id;
                    info.Name = model.Name;
                    info.UrlHost = model.UrlHost;
                    info.DisplayName = model.DisplayName;
                    info.AllowDownload = model.AllowDownload ? (ulong)1 : (ulong)0;
                    info.Downloader = model.Downloader;
                    info.PlayerId = model.PlayerId;
                    info.PositionTag = model.PositionTag;
                    info.ModifyTime = DateTime.Now;

                    SiteBLL.Update(info);

                    return RedirectToAction("Index");
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("Error", ex.Message);
                }
            }

            return View(model);
        }
    }
}